import OSS from "ali-oss"
import {
	aliOSSConfig
} from "./config.js"
const client = new OSS(aliOSSConfig);
import {
	Message
} from 'element-ui';

let tempCheckpoint;
const alioss = {
	// 简单上传
	"put": async (file, fileName) => { 
		try {
			let result = await client.put(fileName, file);
			return result;
		} catch (err) {
			Message.error(err)
		}
	},

	//断点续传(>100M使用)
	"multipartUpload": async (file, fileName) => { 
		try {
			let result = await client.multipartUpload(fileName, file, {
				progress: function(p, checkpoint) {
					// 断点记录点。浏览器重启后无法直接继续上传，您需要手动触发继续上传。
					tempCheckpoint = checkpoint;
				}
			})
		} catch (err) {
			Message.error(err)
		}
	},
	 //暂停上传
	"stopMultipartUpload": async () => {
		client.cancel();
	},
	// 恢复上传
	"resumeUpload": async (file, fileName) => { 
		try {
			let result = await resumeclient.multipartUpload(fileName, file, {
				progress: function(p, checkpoint) {
					tempCheckpoint = checkpoint;
				},
				checkpoint: tempCheckpoint
			})
		} catch (err) {
			Message.error(err)
		}
	},


	// 获取的内容是ArryBuffer
	"get": async (fileName) => {
		try {
			let result = await client.get(fileName);
			return result;
		} catch (e) {
			Message.error(err)
		}
	},

	// 获取下载文档的url（有效时长3600秒）web使用这个下载
	"getDownloadUrl": function(fileName) {
		let downloadUrl = client.signatureUrl(fileName);
		return downloadUrl;
	},
	
	// 删除单个文件
	"deleteFile": async (fileName) => { 
		try {
			let result = await client.delete(fileName);
			return result;
		} catch (err) {
			Message.error(err)
		}
	},

	// 批量删除
	"deleteMulti": async (...args) => { 
		try {
			let result = await client.deleteMulti([...args], {
				quiet: true  // 是否返回删除结果
			});
			return result;
		} catch (e) {
			Message.error(err)
		}
	}
}

export default alioss;
